//
//  MenuItemCell.m
//  xxx
//
//  Created by simon on 2017/10/23.
//  Copyright © 2017年 com.Microants. All rights reserved.
//

#import "MenuItemCell.h"
#import "UILabel+TopBadge.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import <Masonry/Masonry.h>

@interface MenuItemCell ()


@end

@implementation MenuItemCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        [self initData];
        [self setUI];
        [self addMenuConstraint];
    }
    return self;
}


- (void)initData{
    
    self.imageViewTopToSuperViewSpace = LCDScale_iPhone6(2);
    self.imageViewSquareSideLength = LCDScale_iPhone6(45.f);
    self.titleLabToImageViewSpace =  LCDScale_iPhone6(8);
    self.titleLabBottomToSupViewSpace = 0;
}

- (void)setUI{
    
    [self.contentView addSubview:self.iconImageView];
    [self.contentView addSubview:self.titleLab];
    [self.contentView addSubview:self.badgeLab];
//    self.backgroundColor = UIColor.lightGrayColor;
}

- (void)addMenuConstraint{
    
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
           
        make.centerX.mas_equalTo(self.contentView.mas_centerX);
        make.top.mas_equalTo(self.contentView.mas_top).offset(self.imageViewTopToSuperViewSpace);
        make.size.mas_equalTo(CGSizeMake(self.imageViewSquareSideLength, self.imageViewSquareSideLength));
    }];
    
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.iconImageView.mas_bottom).offset(self.titleLabToImageViewSpace);
        make.centerX.mas_equalTo(self.iconImageView.mas_centerX);
        make.bottom.mas_lessThanOrEqualTo(-self.titleLabBottomToSupViewSpace);
    }];
    
    [self.badgeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.iconImageView.mas_right).offset(-14);
        make.bottom.mas_equalTo(self.iconImageView.mas_top).offset(-12);

    }];
}



- (void)setImageViewTopToSuperViewSpace:(CGFloat)imageViewTopToSuperViewSpace
{
    _imageViewTopToSuperViewSpace = imageViewTopToSuperViewSpace;
}

- (void)setImageViewSquareSideLength:(CGFloat)imageViewSquareSideLength
{
    _imageViewSquareSideLength = imageViewSquareSideLength;
}

- (void)setTitleLabToImageViewSpace:(CGFloat)titleLabToImageViewSpace
{
    _titleLabToImageViewSpace = titleLabToImageViewSpace;
}

- (void)setTitleLabBottomToSupViewSpace:(CGFloat)titleLabBottomToSupViewSpace
{
    _titleLabBottomToSupViewSpace = titleLabBottomToSupViewSpace;
}

 // Configure the cell
- (void)setData:(id)data placeholderImage:(UIImage *)placeholderImage
{
    MenuItemModel *model = (MenuItemModel *)data;
    self.titleLab.text = model.title ;
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:model.icon] placeholderImage:placeholderImage];
    if (model.sideMarkType ==SideMarkTypeNumber)
    {
        self.badgeLab.hidden = NO;
        [self.badgeLab menuTopBadgeIconWithBadgeValue:[model.sideMarkValue integerValue] maginY:1.f badgeFont:[UIFont systemFontOfSize:LCDScale_iPhone6(12)] titleColor:[UIColor whiteColor] backgroundColor:[UIColor redColor]];
        
        if ([model.sideMarkValue integerValue]>99)
        {
            self.badgeLabLeftToIconRightSpace = -17;
        }
        else
        {
            self.badgeLabLeftToIconRightSpace = -14;
        }
    }
    else
    {
        self.badgeLab.hidden = YES;
    }
}


+ (CGFloat)getTitleLabToImageViewDefaultSpace
{
    MenuItemCell *cell = [MenuItemCell new];
    return cell.titleLabToImageViewSpace;
}

+ (CGFloat)getImageViewToSupViewDefaultTop
{
    MenuItemCell *cell = [MenuItemCell new];
    return cell.imageViewTopToSuperViewSpace;
}

+ (CGFloat)getTitleLabToSupViewDefaultBottom
{
    MenuItemCell *cell = [MenuItemCell new];
    return cell.titleLabBottomToSupViewSpace;
}



- (UIImageView *)iconImageView{
    if (!_iconImageView) {
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.clipsToBounds = YES;
        _iconImageView = imageView;
    }
    return _iconImageView;
}

- (UILabel *)titleLab
{
    if (!_titleLab) {
        UILabel *lab = [[UILabel alloc] init];
        lab.font = [UIFont systemFontOfSize:LCDScale_iPhone6(12)];;
        lab.text = NSLocalizedString(@"", nil);
        _titleLab = lab;
    }
    return _titleLab;
}
@end
